from visual import *
from placeFinder import getDistanceFromPosition
from constants import sphereDistance, sin60

collectionBalls = []

def checkForCollection(activeBall, allBalls):
    for index in range(len(allBalls)):
        if (getDistanceFromPosition (allBalls[index].pos, activeBall.pos) <= sphereDistance + 0.1) and (allBalls[index].color == activeBall.color) and (index not in collectionBalls):
            collectionBalls.append(index)
            checkForCollection(allBalls[index], allBalls)   
#trazi koliko ima istobojnih kugli na hrpi

def eraseCollection(allBalls, emptyPlaces):
    elementsDelete = []
    for index in range(len(collectionBalls)):
        rate(5)
        i = collectionBalls[index]
        print ("brise se index", i)
        allBalls[i].color = color.black
        allBalls[i].opacity = 0
        emptyPlaces.append(allBalls[i].pos)
        elementsDelete.append(allBalls[i].pos)
    for index in sorted(collectionBalls, reverse = True):
        del allBalls[index]
#brise te kugle, tj postavlja ih nevidljivima, pa ih brise iz liste allBalls i doda slobodno mjesto u emptyPlaces
#stavio sam da prije nego postanu nevidljive im postavi crnu boju jer sam uocio da vpython pooooonekad nece izbrisati, pa se to vidi tako da ostane crna kugla
        
        
def findLonelyBalls (allBalls):
    for index in range(len(allBalls)):
        flag = index
        for indexCompare in range(len(allBalls)):
            if (getDistanceFromPosition (allBalls[index].pos, allBalls[indexCompare].pos) <= (sphereDistance + 0.1)):
                #print("prekinuto")
                flag = 0
                break
        if (flag != 0):
            print("izbacena kugla bez susjeda")
            del allBalls[index]
            #traži kugle koje su ostale bez susjeda


              
                
